// pages/diary/diary.js
var app = getApp();
Page({

  /** 
   * 页面的初始数据
   */
  data: {
    pageSize: 10, //每页条数
    pageNum: 1, // 页数
    diaryBookId: "", //日记id
    arraydata: "",
    imgUrl: app.globalData.imgUrl,
    localImg: app.globalData.staticimgUrl,  // 本地图片
    BookId: "",
    userdata: "",
    userId: "", //关注用的  用户ID
    time: '', //转的时间
    more: 0, // 是否为上拉加载
    isshow:0,//点赞默认
    options:'',//默认
    total:''
  }, 



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      options
    })
    this.rijishang(options)
    this.riji(options)
  },
  onShow:function(){
    this.riji(this.data.options)
  },

  //美丽日记列表上边内容
  rijishang: function(options) {
   
    var bookid = options.bookid
    var that = this;
    //美丽日记列表上边内容
    app.request({
      url: 'doctor/diaryBook/detail', 
      data: {
        bookId: bookid, //日记本id   
      },
      success(res) {
        that.setData({
          userdata: res.data,
          time: app.timeData(res.data.diaryBook.addTime),
        })
      }
    })
  },
  //美丽日记列表
  riji: function(options) {
   
    var bookid = this.data.options.bookid;
    this.setData({
      bookid:bookid,
    });
  
    var that = this;
    //美丽日记列表
    app.request({
      url: 'doctor/diary/getDiaryList', 
      data: {
        diaryBookId: bookid, //日记本id
        pageSize: that.data.pageSize, //每页条数
        pageNum: that.data.pageNum, // 页数
      },
      success(res) {
        wx.hideLoading();
        that.setData({
          arraydata: res.data,
          total: res.page.total
        })
      }
    })
  },

  //关注按钮
  bindFollow: function (e) {
    var that=this;
    var userid = e.currentTarget.dataset.userid;
    var isfollow = e.currentTarget.dataset.isfollow;
    var ziji = wx.getStorageSync('userid');
    
    if (userid == ziji) {
      wx.showToast({
        title: '不能关注自己',
        icon: 'success',
        duration: 1000
      })
    }
    if (isfollow == 1) {
      app.request({
        url: 'doctor/member/attention',
        data: {
          userId: userid,
        },
        success(res) {
          wx.showToast({
            title: '取消关注',
            icon: 'success',
            duration: 1000
          })
          that.rijishang(that.data.options);
        }
      })
    }
    if (isfollow == 2) {
      app.request({
        url: 'doctor/member/attention',
        data: {
          userId: userid,
        },
        success(res) {
          wx.showToast({
            title: '关注成功',
            icon: 'success',
            duration: 1000
          })
          that.rijishang(that.data.options);
        }
      })
    }
    
  },
  // 处理点赞按钮
  handlePoint: function(e) {
    var that = this;
    if (e.currentTarget.dataset.isshow == 0) {
      app.request({
        url: 'doctor/diary/praise',
        data: {
          diaryId: e.currentTarget.dataset.id,
        },
        success(res) {
          that.setData({
            id: that.data.diaryId,
            isshow: 1,
          });
        }
      })
    }
   
  },
  // 日记浏览量
  browse: function() {
    var that = this;
    app.request({
      url: 'doctor/diary/addDiaryViews', 
      data: {
        userId: that.data.userId, //用户id
      },
      success(res) {}
    })
  },

  
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let { pageSize, total, arraydata}=this.data;
    wx.showLoading({
      title: '加载中...',
    })
    this.setData({
      pageSize: pageSize + 10
    });
    if (arraydata.length == total ){
      wx.showToast({
        title: '没有更多了哦~',
        icon: 'none',
        duration: 1000
      });
    }else{
      this.riji();
    }
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: '分享好友获取美丽币！',
      path: '/pages/index/index',
      success: function() {
        app.request({
          url: 'doctor/memberTaskApi/addBeauty',
          data: {
            userId: wx.getStorageSync('userid'),
            type: 3
          },
          success(res) {
            console.log('分享给美丽币给美丽值', res);
          }
        })
      }
    }
  },
  //单张图片放大
  imgYu: function (event) {
    var src = event.currentTarget.dataset.src; //获取data-src
    var imgUrl = app.globalData.imgUrl;
    var photo = this.data.userdata.photo;
    var urls = [imgUrl + photo.photoOne, imgUrl + photo.photoThree, imgUrl + photo.photoTwo];

    //图片预览
    wx.previewImage({
      current: imgUrl + src, // 当前显示图片的http链接
      urls
    })
  }, 
  //数组图片放大
  imgYu1: function (event) {
    
    var src = event.currentTarget.dataset.src; //获取data-src
    var imgList = event.currentTarget.dataset.list; //获取data-list
    var imgUrl = app.globalData.imgUrl;
    var urls = [];
    for (var i = 0; i<imgList.length; i++) { 
      urls.push(imgUrl + imgList[i]);
    }
    wx.previewImage({
      current: imgUrl + src, 
      urls
    })
  },

})